package cc.shacocloud.luanniao.web.controller

import cc.shacocloud.luanniao.core.model.dto.input.AiModelInput
import cc.shacocloud.luanniao.core.model.po.AiModelPo
import cc.shacocloud.luanniao.core.model.po.NamespacePo
import cc.shacocloud.luanniao.core.service.AiModelService
import cc.shacocloud.luanniao.web.controller.interceptor.NamespaceOwnerAuth
import cc.shacocloud.luanniao.web.controller.interceptor.UserNamespacePermissionInterceptor.Companion.currentUserNamespaceMo
import cc.shacocloud.luanniao.web.model.convert.toOutput
import cc.shacocloud.luanniao.web.model.dto.output.AiModelOutput
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ServerWebExchange

/**
 * ai 模型控制器
 *
 * @author 思追(shaco)
 */
@NamespaceOwnerAuth
@Validated
@RestController
@RequestMapping("/api/namespace/{namespaceId}/ai-model")
class AiModelController(
    val aiModelService: AiModelService,
) {

    /**
     * 创建ai模型记录
     */
    @PostMapping
    suspend fun create(
        @Validated @RequestBody model: AiModelInput,
        exchange: ServerWebExchange,
    ): AiModelOutput {
        val userNamespaceMo = exchange.currentUserNamespaceMo()
        val aiModelPo = aiModelService.create(model, userNamespaceMo.namespace)
        return aiModelPo.toOutput()
    }
}